#include "StdAfx.h"
#include ".\upicture.h"

namespace UI
{
	struct CUSTOMVERTEX7
	{
		D3DXVECTOR3 vecPos;
	};

	uPicture::uPicture()
	{
		m_pDevice = NULL;
		m_pTexture = NULL;
		m_ppDecl	= 0;

		dwVertexType  = D3DFVF_XYZ;
	}

	uPicture::~uPicture(void)
	{
		if(m_pTexture)
			m_pTexture->Release();
	}


	HRESULT uPicture::Load(LPDIRECT3DDEVICE9 pDevice , const char* strFile ,FRECT* pImageSize)
	{
		m_pDevice = pDevice;

		CUSTOMVERTEX7 Vertices[4] =
		{
			{ D3DXVECTOR3(pImageSize->left , pImageSize->bottom    , 0.0f )  },
			{ D3DXVECTOR3(pImageSize->left , pImageSize->top         , 0.0f ) },  
			{ D3DXVECTOR3(pImageSize->right , pImageSize->bottom  , 0.0f ) }, 
			{ D3DXVECTOR3(pImageSize->right , pImageSize->top       , 0.0f ) }
		};

		if( FAILED( m_pDevice->CreateVertexBuffer( 4 * sizeof(CUSTOMVERTEX7), 0, dwVertexType, D3DPOOL_DEFAULT, &m_pVBuff, NULL ) ) )		throw E_FAIL;

		VOID* pVertices;
		if( FAILED( m_pVBuff->Lock( 0, sizeof(Vertices), (void**)&pVertices, 0 ) ) )		throw E_FAIL;

		memcpy( pVertices, Vertices, sizeof(Vertices) );
		m_pVBuff->Unlock();

		D3DXCreateTextureFromFile(pDevice , strFile , &m_pTexture);

		D3DXCreateTextureFromFile(pDevice , "tex_a.tga" , &m_pAlphaTex);

		D3DXMatrixIdentity(&m_matPos);

		m_matPos._11 = 10.0f;
		m_matPos._22 = 10.0f;
		m_matPos._41 = 0.0f;
		m_matPos._42 = 0.0f;

		m_matTex._11 = 1.0f ;
		m_matTex._22 = 1.0f ;
		m_matTex._41 = 1.0f;
		m_matTex._42 = 1.0f;
		fAngle = 0;

		return S_OK;
	}

	void uPicture::SetPos(D3DXVECTOR3 vecPos , D3DXVECTOR2 vecSize)
	{
		D3DXMatrixIdentity(&m_matPos);

		m_matPos._11 = vecSize.x;
		m_matPos._22 = vecSize.y;
		m_matPos._41 = vecPos.x;
		m_matPos._42 = vecPos.y;
	}

	D3DXMATRIX matOriginal;

	void uPicture::Render()
	{
		D3DXMatrixIdentity(&matOriginal);

		fAngle -= 0.001f;
		D3DXMatrixRotationX(&m_matTex , fAngle);

		m_pDevice->SetRenderState(D3DRS_LIGHTING , FALSE);
		m_pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,   TRUE );

		m_pDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION );
		m_pDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );

		m_pDevice->SetTextureStageState(1,  D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION );
		m_pDevice->SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
		m_pDevice->SetTextureStageState(1 , D3DTSS_COLOROP,  D3DTOP_SELECTARG1 );
		m_pDevice->SetTextureStageState(1 , D3DTSS_COLORARG2 , D3DTA_TEXTURE);
		m_pDevice->SetTextureStageState(1 , D3DTSS_COLORARG1 , D3DTA_CURRENT);

		m_pDevice->SetTextureStageState(1 , D3DTSS_ALPHAOP , D3DTOP_SELECTARG1 );
		m_pDevice->SetTextureStageState(1 , D3DTSS_ALPHAARG1 , D3DTA_TEXTURE);
		m_pDevice->SetTextureStageState(1 , D3DTSS_ALPHAARG2 , D3DTA_DIFFUSE);

		m_pDevice->SetTransform(D3DTS_TEXTURE0 , &matOriginal);
		m_pDevice->SetTransform(D3DTS_TEXTURE1 , &m_matTex);
		m_pDevice->SetTexture(0, m_pTexture);
		m_pDevice->SetTexture(1 , m_pAlphaTex);

		m_pDevice->SetStreamSource( 0, m_pVBuff, 0, sizeof(CUSTOMVERTEX7) );
		m_pDevice->SetFVF( dwVertexType );
		m_pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

		m_pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,   FALSE );
		m_pDevice->SetTextureStageState( 0 , D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU );
		m_pDevice->SetTextureStageState( 0 , D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
		m_pDevice->SetTextureStageState( 1 , D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU );
		m_pDevice->SetTextureStageState( 1 , D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
		m_pDevice->SetTextureStageState( 1 , D3DTSS_ALPHAOP,D3DTOP_DISABLE );
		m_pDevice->SetTextureStageState( 1 , D3DTSS_COLOROP,  D3DTOP_DISABLE );

	}

};